﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;

public class Slot : MonoBehaviour ,IPointerEnterHandler,IPointerExitHandler,IPointerDownHandler
{


	// Use this for initialization
	void Start ()
	{
	
	}
	
	// Update is called once per frame
	void Update ()
	{
	
	}

	public int GetGoodID()
	{
		if (this.transform.childCount == 0)
		{
			return -1;
		}
		GoodUI gu = this.transform.GetChild(0).GetComponent<GoodUI>();
		return gu.good.goodProperty.ID;
	}

	public bool IsFill()
	{
		GoodUI gu = this.transform.GetChild(0).GetComponent<GoodUI>();
		return gu.good.goodProperty.capacity <= gu.amount;//物品的容量小于物品当前的数量
	}


	public GameObject goodPrefab;
	public void StoreGood(KnapsackGood good,int amount=1)
	{
		if (this.transform.childCount == 0)//如果该物品槽为空，则在这物品槽里新建一个GoodUI
		{
			GameObject obj = GameObject.Instantiate(goodPrefab);
			obj.transform.SetParent(this.transform);
			obj.transform.localPosition = Vector3.zero;
			obj.transform.localScale = Vector3.one;

			obj.GetComponent<GoodUI>().SetGood(good,amount);
		}
		else
		{
			if (this.transform.GetChild(0).gameObject.activeInHierarchy == false)
			{
				this.transform.GetChild(0).GetComponent<GoodUI>().Show();
				this.transform.GetChild(0).GetComponent<GoodUI>().SetGood(good,amount);
			}
			else
			{
				this.transform.GetChild(0).GetComponent<GoodUI>().AddGood();
			}
			
		}
	}

	public  void OnPointerEnter(PointerEventData eventData)
	{
		Debug.Log("鼠标进入物品槽");
		if (this.transform.childCount > 0)
		{
			//获得描述文本
			string text = this.transform.GetChild(0).GetComponent<GoodUI>().good.GetDescribe();
		
			KnapsackManager.GetInstance.ShowToolTilePanel(text);
		}
	}

	public void OnPointerExit(PointerEventData eventData)
	{
		if (this.transform.childCount > 0)
		{
			KnapsackManager.GetInstance.HideToolTilePanel();
		}
	}

	/// <summary>
	/// 鼠标抬下
	/// 
	/// **************************方法说明*************************************
	/// *//物品槽里没有物品													  *
	///	*	//1.IsPickGood=true		将PickGood里的物品放置到该物品槽内		  *
	///	*	//2.IsPickGood=false	不做任何处理							  *
	///	*//物品槽里存在物品													  *					
	///	*	//1.IsPickGood=true		将该物品槽的物品替换PickGood里的物品		  *	
	///	*	//2.IsPickGood=false	将该物品槽的物品放置在PickGood里		  *
	///	***********************************************************************
	/// </summary>
	/// <param name="eventData"></param>
	public virtual void OnPointerDown(PointerEventData eventData)
	{
		if (this.transform.childCount == 0 || this.transform.GetChild(0).gameObject.activeInHierarchy == false)
		{
			if (KnapsackManager.GetInstance.isPickGood)
			{
				Debug.Log(KnapsackManager.GetInstance.GetPickGoodUI.amount);
				StoreGood(KnapsackManager.GetInstance.GetPickGoodUI.good, KnapsackManager.GetInstance.GetPickGoodUI.amount);
				
				KnapsackManager.GetInstance.GetPickGoodUI.SetLocalPosition(new Vector3(-420,2,0));//将pickGoodUI位置归零

				KnapsackManager.GetInstance.isPickGood = false;
			}
			else
			{
				//无需做处理
			}
		}
		else
		{
			if (KnapsackManager.GetInstance.isPickGood)
			{
				StorageSlot.slot.StoreGood(this.transform.GetChild(0).GetComponent<GoodUI>().good, this.transform.GetChild(0).GetComponent<GoodUI>().amount);
				
				//this.transform.GetChild(0).GetComponent<GoodUI>().Exchange(Slot.GetInstance.transform.GetChild(0).GetComponent<GoodUI>());
				//StoreGood(KnapsackManager.GetInstance.GetPickGoodUI.good, KnapsackManager.GetInstance.GetPickGoodUI.amount);
				this.transform.GetChild(0).GetComponent<GoodUI>().SetGood(KnapsackManager.GetInstance.GetPickGoodUI.good, KnapsackManager.GetInstance.GetPickGoodUI.amount);

				KnapsackManager.GetInstance.GetPickGoodUI.SetLocalPosition(new Vector3(-420, 2, 0));//将pickGoodUI位置归零

				KnapsackManager.GetInstance.isPickGood = false;
			}
			else
			{
				StorageSlot.slot = this;

				KnapsackManager.GetInstance.GetPickGoodUI.ReplaceGoodUI(transform.GetChild(0).GetComponent<GoodUI>());

				KnapsackManager.GetInstance.isPickGood = true;

				//将自身物品槽的UI销毁
                Destroy(this.transform.GetChild(0).gameObject);
			}
		}
	}
}
